//
//  DoubleTicket.m
//  MySwift
//
//  Created by 周杨 on 14/11/2.
//  Copyright (c) 2014年 zhouy. All rights reserved.
//

#import "DoubleTicket.h"
#import "DoubleColorBall.h"

@implementation DoubleTicket

//私有排序方法
-(NSMutableArray *) sortBallArr:(NSMutableArray *) arr{

    //冒泡排序，并且篮球在最后面
    for ( int i = 0,size = (int)[arr count] ;i<=size;++i ){
        for (int y = size-1;y>i;y--){
            
            DoubleColorBall * pObj = arr[y];
            DoubleColorBall * nObj = arr[y-1];
            
            
            if( [nObj No]>[pObj No]){
                DoubleColorBall * tObj = arr[y];
                
                arr[y]=nObj;
                arr[y-1]=tObj;
            }
        }
    }
    
    return arr;
}

-(NSString *) ticketStr{
    NSMutableArray * redArr = [[NSMutableArray alloc] init];
    NSMutableArray * blueArr =[[NSMutableArray alloc] init];
    
    for (DoubleColorBall * ball in [self reBallArry]) {
        if ([[ball Color]  isEqual:@"Red"]) {
            [redArr addObject:ball];
        } else {
            [blueArr addObject:ball];
        }
    }
    
    NSMutableArray * tempArr = [[NSMutableArray alloc] init];
    
    [tempArr addObjectsFromArray:[self sortBallArr:redArr ]];
    [tempArr addObjectsFromArray:[self sortBallArr:blueArr ]];
    
    [self setReBallArry:tempArr];
    
    
    NSString * reStr = @"";
    
    for (int i=0,size=(int)[[self reBallArry] count] ; i<size; i++ ) {
        reStr=[reStr stringByAppendingFormat:@" %d ", [[self reBallArry][i] No]];

        
        //reStr=[reStr stringByAppendingFormat:@"%@:%d  ",[[self reBallArry][i] Color], [[self reBallArry][i] No]];
    }
    
    return reStr;
}


-(NSString *) ticketSortStr{
    NSMutableArray * redArr = [[NSMutableArray alloc] init];
    
    for (DoubleColorBall * ball in [self reBallArry]) {
        if ([[ball Color]  isEqual:@"Red"]) {
            [redArr addObject:ball];
        }
    }
    
    NSString * reStr = @"Red:";
    
    for (int i=0,size=(int)[redArr count] ; i<size; i++ ) {
        reStr=[reStr stringByAppendingFormat:@" %d ",[redArr[i] No]];
    }
    
    return reStr;
}

//返回 红球和值
-(int) getRedSum{
    
    int sum = 0 ;
    
    
    
    for (DoubleColorBall * ball in [self reBallArry]) {
        if ([[ball Color]  isEqual:@"Red"]) {
            sum = sum+ [ball No];
        }
    }
    
    
    return sum;
}

@end
